/**=========================================================
 * Service: simple-check.js
 =========================================================*/

App.service('SimpleCheck', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            init: function($scope,options) {
                var defaultOptions = {
                    optionModel: '',
                    initValue: false,
                    checkChangeEvent: null
                }
                
                var finalOptions = angular.extend(defaultOptions,options)
                
                if (utils.isEmpty($scope.checkboxs)) {
                    $scope.checkboxs = {}
                }
                
                $scope.checkboxs[finalOptions.optionModel] = finalOptions.initValue;
                
                if (options.checkChangeEvent instanceof Function) {
                    finalOptions.checkChangeEvent = options.checkChangeEvent;
                    $scope.$watch('checkboxs.' + finalOptions.optionModel, 
                        function (newVal, oldVal) { 
                            if (newVal != oldVal){
                                options.checkChangeEvent.call();
                            }
                        }
                    );
                }

                return {
                    scope: $scope,
                    options: finalOptions,
                    getCheck: function(){
                        var options = this.options;
                        var scope = this.scope;
                        return scope.checkboxs[options.optionModel];
                    }
                }
            }
        }
    }
]);